package com.vma.inter.controller.lottery;

import com.jfinal.core.Controller;
import com.vma.inter.util.ReturnsUtil;
import com.vma.lottery.common.ChineseDate;
import com.vma.lottery.model.LotteryRecord;
import com.vma.lottery.model.Predict;

import java.util.Calendar;

/**
 * Created by chenzui on 2017/3/27.
 */
public class LotteryController extends Controller {
    public void predict(){
        try{
            renderJson(ReturnsUtil.returnSuccess(Predict.dao.findFirst("select * from predict order by id desc")));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }


    /**
     * 按年份分页查询开奖记录
     */
    public void qryLotteryRecordList(){
        try{
            String year = getPara("year");
            String cPage = getPara("cPage");
            String pSize = getPara("pSize");
            renderJson(ReturnsUtil.returnSuccess(LotteryRecord.dao.qryLotteryRecordList(year,cPage,pSize)));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 查询最新的一条开奖记录
     */
    public void gryLatestRecord(){
        try{
            renderJson(ReturnsUtil.returnSuccess(LotteryRecord.dao.gryLatestRecord()));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 查询下一期开奖时间
     */

    public void gryNextTime(){
        LotteryRecord lotteryRecord = LotteryRecord.dao.findFirst("select * from lottery_record order by opentime desc");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(lotteryRecord.getOpentime());
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        switch (day){
            case 1:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+2); break;
            case 2:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+1); break;
            case 3:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+2); break;
            case 4:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+1); break;
            case 5:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+2); break;
            case 6:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+1); break;
            case 7:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+3); break;

        }
        String str = "第"+lotteryRecord.getExpect()+"期 "+calendar.get(Calendar.YEAR)+"-"+
                (calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.DATE)+
                " 21点30分 星期"+getDay(calendar.get(Calendar.DAY_OF_WEEK));
        renderJson(ReturnsUtil.returnSuccess(str));
    }


    public void number2sx(){
        renderJson(ReturnsUtil.returnSuccess(ChineseDate.number2sx()));
    }
    private String getDay(int day){
        String reStr="";
        switch (day){
            case 1:reStr="日";break;
            case 2:reStr="一";break;
            case 3:reStr="二";break;
            case 4:reStr="三";break;
            case 5:reStr="四";break;
            case 6:reStr="五";break;
            case 7:reStr="六";break;
        }
        return reStr;
    }
    public void gryPredict(){
        try{
            Predict predict = Predict.dao.findFirst("select * from predict order by id desc");
            renderJson(ReturnsUtil.returnSuccess(predict));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException("gryPredict",e));
        }
    }

}
